/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.utils.miscs;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.sencha.gxt.core.client.resources.ThemeStyles;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;


/**
 * UI Utils
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class UIUtils
{

	/**
	 * Update style for bottom {@link ToolBar} (border and Theme)
	 * 
	 * @param toolbar
	 *           the {@link ToolBar}
	 */
	public static final void updateBottomToolbarStyle(final ToolBar toolbar)
	{
		toolbar.addStyleName(ThemeStyles.getStyle().borderTop());
		toolbar.getElement().getStyle().setProperty("borderBottom", "none");
	}

	/**
	 * Update border display of given component
	 * 
	 * @param component
	 *           the component
	 * @param topBorderVisible
	 *           a boolean indicating whether top border should be displayed
	 * @param rightBorderVisible
	 *           a boolean indicating whether right border should be displayed
	 * @param bottomBorderVisible
	 *           a boolean indicating whether bottom border should be displayed
	 * @param leftBorderVisible
	 *           a boolean indicating whether left border should be displayed
	 */
	public static final void setBorder(final Component component, final boolean topBorderVisible,
			final boolean rightBorderVisible, final boolean bottomBorderVisible, final boolean leftBorderVisible)
	{
		setBorderTop(component, topBorderVisible);
		setBorderRight(component, rightBorderVisible);
		setBorderBottom(component, bottomBorderVisible);
		setBorderLeft(component, leftBorderVisible);
	}

	/**
	 * Update border display of given component
	 * 
	 * @param component
	 *           the component
	 * @param visible
	 *           a boolean indicating whether top border should be displayed
	 */
	public static final void setBorderTop(final Component component, final boolean visible)
	{
		if (visible)
		{
			component.addStyleName(ThemeStyles.getStyle().borderTop());
		}
		else
		{
			component.getElement().getStyle().setProperty("borderTop", "none");
		}
	}

	/**
	 * Update right border display of given component
	 * 
	 * @param component
	 *           the component
	 * @param visible
	 *           a boolean indicating whether right border should be displayed
	 */
	public static final void setBorderRight(final Component component, final boolean visible)
	{
		if (visible)
		{
			component.addStyleName(ThemeStyles.getStyle().borderRight());
		}
		else
		{
			component.getElement().getStyle().setProperty("borderRight", "none");
		}
	}

	/**
	 * Update bottom border display of given component
	 * 
	 * @param component
	 *           the component
	 * @param visible
	 *           a boolean indicating whether bottom border should be displayed
	 */
	public static final void setBorderBottom(final Component component, final boolean visible)
	{
		if (visible)
		{
			component.addStyleName(ThemeStyles.getStyle().borderBottom());
		}
		else
		{
			component.getElement().getStyle().setProperty("borderBottom", "none");
		}
	}

	/**
	 * Update left border display of given component
	 * 
	 * @param component
	 *           the component
	 * @param visible
	 *           a boolean indicating whether left border should be displayed
	 */
	public static final void setBorderLeft(final Component component, final boolean visible)
	{
		if (visible)
		{
			component.addStyleName(ThemeStyles.getStyle().borderLeft());
		}
		else
		{
			component.getElement().getStyle().setProperty("borderLeft", "none");
		}
	}

	/**
	 * Change grid content enable status
	 * 
	 * @param grid
	 *           the grid
	 * @param enabled
	 *           whether the grid should be enabled or not
	 */
	public static final void setGridContentEnabled(final Grid<?> grid, final boolean enabled, final String disabledStyle)
	{
		final Node node = grid.getElement().getFirstChild().getNextSibling().getFirstChild();
		if (node.getNodeType() == Node.ELEMENT_NODE)
		{
			final Element token = (Element) node;
			if (enabled)
			{
				token.removeClassName(disabledStyle);
			}
			else
			{
				token.addClassName(disabledStyle);
			}
		}
	}

}
